package com.example.backend.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LoginTimeAspect {
    @Pointcut("@annotation(com.example.backend.annotation.constraint.LogTime)")
    public void loginTime() {
    }

    @Around("loginTime()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed();
            long endTime = System.currentTimeMillis();
            log.debug("Method {} executed in {} ms", joinPoint.getSignature().toShortString(), endTime - startTime);
            return result;
        } catch (Throwable e) {
            long endTime = System.currentTimeMillis();
            log.error("Method {} failed after {} ms", joinPoint.getSignature().toShortString(), endTime - startTime, e);
            throw e;
        }
    }
}
